package com.cloud.pole.shop.biz.shops;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.mapper.goods.ShopGoodsCategoryRecommMapper;
import com.cloud.pole.mapper.goods.ShopGoodsModelAttrMapper;
import com.cloud.pole.mapper.shops.ShopShopsViewMapper;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsCategoryRecomm;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsModelAttr;
import com.cloud.pole.shop.model.entity.shops.ShopShopsFans;
import com.cloud.pole.shop.model.entity.shops.ShopShopsView;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopShopsViewBiz extends BaseBiz<ShopShopsViewMapper, ShopShopsView> {
	
	@Cache(key="shopShopsViewList{1}-shopId-fans")
    public List<ShopShopsView> selectTopListByShopId(Integer shopId, Integer topNumber) {
		 PageHelper.startPage(1, topNumber); 
		 Example example = new Example(ShopShopsView.class);
		 Criteria criteria = example.createCriteria();
		 example.setOrderByClause("last_view_time desc");
		 criteria.andEqualTo("shopId", shopId);
		 return mapper.selectByExample(example);
    }
	
    public List<ShopShopsView> selectListByShopId(Integer shopId) {
		 Example example = new Example(ShopShopsView.class);
		 Criteria criteria = example.createCriteria();
		 example.setOrderByClause("last_view_time desc");
		 criteria.andEqualTo("shopId", shopId);
		 return mapper.selectByExample(example);
    }
    
    public List<ShopShopsView> selectListByOpenId(String openId) {
		 Example example = new Example(ShopShopsView.class);
		 Criteria criteria = example.createCriteria();
		 example.setOrderByClause("last_view_time desc");
		 criteria.andEqualTo("openId", openId);
		 return mapper.selectByExample(example);
    }
    
    public ShopShopsView selectLastViewShop(Long userId, String openId) {
		 PageHelper.startPage(1, 1); 
		 Example example = new Example(ShopShopsView.class);
		 Criteria criteria = example.createCriteria();
		 example.setOrderByClause("last_view_time desc");
		 criteria.andEqualTo("openId", openId);
		 if(userId != null) {
			 criteria.andEqualTo("userId", userId);
		 }
		 List<ShopShopsView> list = mapper.selectByExample(example);
		 if(list != null && list.size() > 0) {
			 return list.get(0);
		 }
		 return null;
    }
	
    public ShopShopsView selectByShopIdAndOpenId(Integer shopId, String openId) {
		ShopShopsView fans = new ShopShopsView();
		fans.setShopId(shopId);
		fans.setOpenId(openId);	
	    return mapper.selectOne(fans);
    }
	
	@CacheClear(key = "shopShopsViewList{1.shopId}-shopId-fans")
	public void insertSelective(ShopShopsView record) {
		mapper.insertSelective(record);
	}

	@CacheClear(key = "shopShopsViewList{1.shopId}-shopId-fans")
	@Override
	public void updateSelectiveById(ShopShopsView record) {
		super.updateSelectiveById(record);
	}
}
